package com.skit.main.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.skit.common.bean.ActivityBean
import com.skit.main.R
import com.skit.main.bean.RecyclerTypeBean
import com.skit.main.bean.RecyclerTypeBean.Type.*
import kotlinx.android.synthetic.main.re_activity_type_item.view.*
import kotlinx.android.synthetic.main.recycler_type_item.view.*
import kotlinx.android.synthetic.main.recycler_type_item.view.text
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author shuike [Contact me.](mailto:shuike007@gmail.com)
 * @version 1.0
 * @since 2020/5/19
 */

class TypeRecyclerViewAdapter(private val list: MutableList<RecyclerTypeBean>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
//    enum class Type {
//        TYPE, ACTIVITY
//    }

    private val format = SimpleDateFormat("yyyy.MM.dd", Locale.getDefault())

    var onItemClickListener: ((View, Int) -> Unit?)? = null

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title: TextView = itemView.text
        val moreTv: TextView = itemView.moreTv
        val recyclerView: RecyclerView = itemView.recyclerView
    }

//    class TextIconType(itemView: View) : RecyclerView.ViewHolder(itemView) {
//        val text: TextView = itemView.text
//        val icon: ImageView = itemView.icon
//    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context)
                .inflate(R.layout.recycler_type_item, parent, false)
        )
//        inflate.number_of_apply.visibility = View.GONE
//        return when (viewType) {
//            RecyclerTypeBean.Type.ACTIVITY_HOLDER.ordinal -> ViewHolder(
//                LayoutInflater.from(parent.context)
//                    .inflate(R.layout.main_re_item_activity_layout, parent, false)
//            )
//
//            RecyclerTypeBean.Type.TEXT_ICON_HOLDER.ordinal -> TextIconType(
//                LayoutInflater.from(parent.context)
//                    .inflate(R.layout.recycler_type_item, parent, false)
//            )
//
//            else -> ViewHolder(
//                LayoutInflater.from(parent.context)
//                    .inflate(R.layout.main_re_item_activity_layout, parent, false)
//            )
//        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
//        if (holder is ViewHolder) {
//            viewHolder(holder, position)
//        }
        when (holder) {
            is ViewHolder -> {
                viewHolder(holder, position)
            }
        }
    }

    private fun viewHolder(holder: ViewHolder, position: Int) {
        val recyclerTypeBean = list[position]
        holder.title.text = recyclerTypeBean.name
        if (recyclerTypeBean.more == null) {
            holder.moreTv.visibility = View.GONE
        } else {
            holder.moreTv.visibility = View.VISIBLE
            holder.moreTv.text = recyclerTypeBean.more
        }

        holder.moreTv.setOnClickListener {
            recyclerTypeBean.click?.invoke()
        }

        holder.itemView.setBackgroundColor(recyclerTypeBean.backGroundColor)

        holder.recyclerView.apply {
            val mLayoutManager: RecyclerView.LayoutManager =
                when (recyclerTypeBean.layoutManager) {
                    RecyclerTypeBean.LayoutManager.LINEAR_LAYOUT_MANAGER -> object :
                        LinearLayoutManager(context) {
                        override fun canScrollHorizontally(): Boolean {
                            return recyclerTypeBean.scroll
                        }
                    }.apply {
                        orientation = recyclerTypeBean.orientation
                    }
                    RecyclerTypeBean.LayoutManager.GRIDLAYOUT_MANAGER -> object :
                        GridLayoutManager(context, 2) {
                        override fun canScrollVertically(): Boolean {
                            return recyclerTypeBean.scroll
                        }
                    }
                    else -> object : LinearLayoutManager(context) {
                        override fun canScrollHorizontally(): Boolean {
                            return recyclerTypeBean.scroll
                        }
                    }.apply {
                        orientation = recyclerTypeBean.orientation
                    }
                }
            if (mLayoutManager is LinearLayoutManager && mLayoutManager !is GridLayoutManager) {
                if (itemDecorationCount == 0) {

                }
            }
            layoutManager = mLayoutManager
//            }
            setHasFixedSize(false)
            adapter = getMAdapter(recyclerTypeBean)
        }
    }

    private fun getMAdapter(recyclerTypeBean: RecyclerTypeBean): RecyclerView.Adapter<RecyclerView.ViewHolder>? {

        return when (recyclerTypeBean.type) {
            ACTIVITY_HOLDER -> {
                MainActivityRecyclerViewAdapter(recyclerTypeBean.data as List<ActivityBean>).apply {
                    onItemClickListener = this@TypeRecyclerViewAdapter.onItemClickListener
                }
            }
            TEXT_ICON_HOLDER ->
                ReActivityTypeAdapter(recyclerTypeBean.data as List<String>)
        }
    }

    override fun getItemCount(): Int = list.size
}